//
//  NegativeStackedBarChartViewController.m
//  cocoapodText
//
//  Created by apple on 16/8/6.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "ShiPei.h"
//导入时需要手动打上，系统没有提示
#import "Charts-Swift.h"
#import <Foundation/Foundation.h>
#import "NegativeStackedBarChartViewController.h"

@interface NegativeStackedBarChartViewController () <ChartViewDelegate>

@end

@implementation NegativeStackedBarChartViewController
HorizontalBarChartView *sampleNegativeStackedBarChartView;
- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    [self initChartView];
}
-(void)initChartView{
    
    
    sampleNegativeStackedBarChartView=[[HorizontalBarChartView alloc] initWithFrame:CGRectMake(kScreenW/4, kScreenH/4, kScreenW/2, kScreenH/2)];
    sampleNegativeStackedBarChartView.backgroundColor = [UIColor whiteColor];
    sampleNegativeStackedBarChartView.delegate = self;
    [self.view addSubview:sampleNegativeStackedBarChartView];
    [self initHorizontalBarChartStyleAndData];
}

-(void)initHorizontalBarChartStyleAndData{
    [self setupHorizontalBarChartView:sampleNegativeStackedBarChartView];
    [self setHorizontalBarChartParam:sampleNegativeStackedBarChartView];
    [self setLegendStyle:sampleNegativeStackedBarChartView];
    [self initChartData];
}

- (void)setupHorizontalBarChartView:(HorizontalBarChartView *)chartView{
    chartView.descriptionText = @"";
    chartView.noDataTextDescription = @"You need to provide data for the chart.";
    
    chartView.drawBarShadowEnabled = NO;
    chartView.drawValueAboveBarEnabled = YES;
    
    // scaling can now only be done on x- and y-axis separately
    chartView.pinchZoomEnabled = NO;
    
    chartView.drawBarShadowEnabled = NO;
    chartView.drawValueAboveBarEnabled = YES;
}

-(void)setHorizontalBarChartParam:(HorizontalBarChartView *)chartView{
    NSNumberFormatter *customFormatter = [[NSNumberFormatter alloc] init];
    customFormatter.negativePrefix = @"";
    customFormatter.positiveSuffix = @"m";
    customFormatter.negativeSuffix = @"m";
    customFormatter.minimumSignificantDigits = 1;
    customFormatter.minimumFractionDigits = 1;
    
    
    chartView.leftAxis.enabled = NO;
    chartView.rightAxis.axisMaxValue = 25.0;
    chartView.rightAxis.axisMinValue = -25.0;
    chartView.rightAxis.drawGridLinesEnabled = NO;
    chartView.rightAxis.drawZeroLineEnabled = YES;
    chartView.rightAxis.labelCount = 7;
    chartView.rightAxis.valueFormatter = customFormatter;
    chartView.rightAxis.labelFont = [UIFont systemFontOfSize:9.f];
    
    ChartXAxis *xAxis = chartView.xAxis;
    xAxis.labelPosition = XAxisLabelPositionBothSided;
    xAxis.drawGridLinesEnabled = NO;
    xAxis.drawAxisLineEnabled = NO;
    chartView.rightAxis.labelFont = [UIFont systemFontOfSize:9.f];
    
    
}

//设置图例样式
-(void)setLegendStyle:(HorizontalBarChartView *)chartView{
    
    ChartLegend *l = chartView.legend;
    l.position = ChartLegendPositionBelowChartRight;
    l.formSize = 8.f;
    l.formToTextSpace = 4.f;
    l.xEntrySpace = 6.f;
}

- (void)initChartData
{
    [self setChartData:sampleNegativeStackedBarChartView];
}



- (void)setChartData:(HorizontalBarChartView *)chartView
{
    NSMutableArray *yValues = [NSMutableArray array];
    [yValues addObject:[[BarChartDataEntry alloc] initWithValues:@[ @-10, @10 ] xIndex: 0]];
    [yValues addObject:[[BarChartDataEntry alloc] initWithValues:@[ @-12, @13 ] xIndex: 1]];
    [yValues addObject:[[BarChartDataEntry alloc] initWithValues:@[ @-15, @15 ] xIndex: 2]];
    [yValues addObject:[[BarChartDataEntry alloc] initWithValues:@[ @-17, @17 ] xIndex: 3]];
    [yValues addObject:[[BarChartDataEntry alloc] initWithValues:@[ @-19, @20 ] xIndex: 4]];
    [yValues addObject:[[BarChartDataEntry alloc] initWithValues:@[ @-19, @19 ] xIndex: 5]];
    [yValues addObject:[[BarChartDataEntry alloc] initWithValues:@[ @-16, @16 ] xIndex: 6]];
    [yValues addObject:[[BarChartDataEntry alloc] initWithValues:@[ @-13, @14 ] xIndex: 7]];
    [yValues addObject:[[BarChartDataEntry alloc] initWithValues:@[ @-10, @11 ] xIndex: 8]];
    [yValues addObject:[[BarChartDataEntry alloc] initWithValues:@[ @-5, @6 ] xIndex: 9]];
    [yValues addObject:[[BarChartDataEntry alloc] initWithValues:@[ @-1, @2 ] xIndex: 10]];
    
    BarChartDataSet *set = nil;
    if (chartView.data.dataSetCount > 0)
    {
        set = (BarChartDataSet *)chartView.data.dataSets[0];
        set.yVals = yValues;
        [chartView notifyDataSetChanged];
    }
    else
    {
        set = [[BarChartDataSet alloc] initWithYVals:yValues label:@"Age Distribution"];
        set.valueFormatter = chartView.rightAxis.valueFormatter;
        set.valueFont = [UIFont systemFontOfSize:7.f];
        set.axisDependency = AxisDependencyRight;
        set.barSpace = 0.4f;
        set.colors = @[
                       [UIColor colorWithRed:67/255.f green:67/255.f blue:72/255.f alpha:1.f],
                       [UIColor colorWithRed:124/255.f green:181/255.f blue:236/255.f alpha:1.f]
                       ];
        set.stackLabels = @[
                            @"Men", @"Women"
                            ];
        
        NSArray *xVals = @[ @"0-10", @"10-20", @"20-30", @"30-40", @"40-50", @"50-60", @"60-70", @"70-80", @"80-90", @"90-100", @"100+" ];
        
        BarChartData *data = [[BarChartData alloc] initWithXVals:xVals dataSet:set];
        chartView.data = data;
        [chartView setNeedsDisplay];
    }
}


#pragma mark - ChartViewDelegate

- (void)chartValueSelected:(ChartViewBase * __nonnull)chartView entry:(ChartDataEntry * __nonnull)entry dataSetIndex:(NSInteger)dataSetIndex highlight:(ChartHighlight * __nonnull)highlight
{
    NSLog(@"chartValueSelected, stack-index %ld", (long)highlight.stackIndex);
}

- (void)chartValueNothingSelected:(ChartViewBase * __nonnull)chartView
{
    NSLog(@"chartValueNothingSelected");
}

@end
